import 'package:shared_preferences/shared_preferences.dart';

class SPreferences {


  static final SPreferences _instance = SPreferences.instance();
  factory SPreferences()=> _instance;
  SPreferences.instance() ;
  static SharedPreferences? _prefs;

  static Future<void> init() async{
    if(_prefs == null){
      _prefs = await SharedPreferences.getInstance();
    }
  }

  // 设置数字类型
  Future<bool> setInt(String key, int? value) async {
    return await _prefs!.setInt(key, value!);
  }

  // 设置bool
  Future<bool> setBool(String key, bool value) async {
    return await _prefs!.setBool(key, value);
  }

  // 设置String
  Future<bool> setString(String key, String? value) async {
    return await _prefs!.setString(key, value!);
  }

  String getString(String key) {
    return _prefs!.getString(key)??'';
  }

  int getInt(String key) {
    return _prefs!.getInt(key)??0;
  }

  bool? getBool(String key,bool isSet) {
    if (!(_prefs!.containsKey(key))) {
      if(isSet){
        _prefs!.setBool(key, true);
      }
      return false;
    }
    return _prefs!.getBool(key);
  }

  ///删除数据
  Future<bool> deleteData(String key) async{
   return await _prefs!.remove('$key');
  }

}

